package com.feituan.manager.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feituan.core.persistence.service.ISysFileService;
import com.feituan.core.pojo.entity.SysFile;
import com.feituan.core.pojo.request.PageBaseReq;
import com.feituan.core.pojo.response.Response;
import com.feituan.core.pojo.response.page.PageData;
import com.feituan.manager.storage.Storage;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/sys_file")
public class SysFileController {

    @Resource
    private Storage storage;

    @Resource
    private ISysFileService sysFileService;

    @PostMapping("/save")
    public Response<Boolean> save(MultipartFile file, String remark) {
        try {
            String originalFilename = file.getOriginalFilename();
            String keyName = generateKey(originalFilename);
            storage.store(file.getInputStream(), file.getSize(), file.getContentType(), keyName);
            String url = generateUrl(keyName);

            // 新增
            SysFile sysFile = new SysFile();
            sysFile.setFileType(file.getContentType());
            sysFile.setKeyName(keyName);
            sysFile.setOriginalName(originalFilename);
            sysFile.setUrl(url);
            sysFile.setRemark(remark);
            sysFileService.getBaseMapper().insert(sysFile);
            return Response.success(true);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
    }

    @PostMapping("/list_page")
    public Response<PageData<SysFile>> selectList(PageBaseReq pageBaseReq) {
        Page<SysFile> page = new Page<>();
        page.setCurrent(pageBaseReq.getPage());
        page.setSize(pageBaseReq.getLimit());
        LambdaQueryWrapper<SysFile> queryWrapper = new LambdaQueryWrapper<>();
        Page<SysFile> sysFilePage = sysFileService.getBaseMapper().selectPage(page, queryWrapper);
        return Response.success(PageData.build(sysFilePage.getCurrent(), sysFilePage.getTotal(), sysFilePage.getRecords()));
    }


    @PostMapping("/delete")
    public Response<Boolean> delete(Long id) {
        try {
            SysFile sysFile = sysFileService.getBaseMapper().selectById(id);
            String keyName = sysFile.getKeyName();
            storage.delete(keyName);
            sysFileService.getBaseMapper().deleteById(id);
            return Response.success(true);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    private String generateKey(String originalFilename) {
        int index = originalFilename.lastIndexOf('.');
        String suffix = originalFilename.substring(index);

        return  "static/" + UUID.randomUUID()+suffix;
    }
    private String generateUrl(String keyName) {
        return storage.generateUrl(keyName);
    }



}
